﻿@inject IHttpService _httpService
@inject IJSRuntime JS
@implements IAsyncDisposable
@inject IDataCacheService _dataCatcheService

<section class="recommends" id="recommends">
    <MContainer>
        @if (_personalRecommends.Any())
        {
            <h2>随机推荐</h2>
        }
        <div class="recommends-group">
            @foreach (var item in _personalRecommends)
            {
                <div class="recommends-item">
                    @if (item.DisplayType == PersonalRecommendDisplayType.PlainText)
                    {
                        <PlainTextCard Model="item" />
                    }
                    else if (item.DisplayType == PersonalRecommendDisplayType.ImageGames)
                    {
                        <ImageGamesCard Model="item" />
                    }
                    else if (item.DisplayType == PersonalRecommendDisplayType.Gallery)
                    {
                        <GalleryCard Model="item" />
                    }
                </div>
            }
        </div>
        @if (loading)
        {
            <CnGalWebSite.Components.Progressings.ProgressingCard Class="mt-8" />
        }
        @if (completed)
        {
            <div class="completed-card">
                <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Segmented.DivideCard />
                <div class="content">不能再往下划啦 o(〃＾▽＾〃)o</div>
                    <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Segmented.DivideCard />
            </div>
        }
    </MContainer>

</section>
    @if (completed)
    {
            <CnGalWebSite.Shared.MasaComponent.PC.Home.Cards.Segmented.AboutCard />
    }



@code {

    [Parameter]
    public bool Lock { get; set; }

    List<PersonalRecommendModel> _personalRecommends { get; set; } = new List<PersonalRecommendModel>();

    bool loading;
    bool completed;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private DotNetObjectReference<MainCard> objRef;


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {

            objRef = DotNetObjectReference.Create(this);
            await TriggerDotNetInstanceMethod();
        }
    }
    [JSInvokable]
    public async Task LoadMore()
    {
        try
        {
            if(Lock)
            {
                return;
            }
            if (completed)
            {
                return;
            }
            if (loading)
            {
                return;
            }
            loading = true;
            StateHasChanged();

            var result = await _httpService.PostAsync<IEnumerable<int>, List<PersonalRecommendModel>>("api/home/GetPersonalizedRecommendations", _personalRecommends.Select(s => s.Id));

            if (result.Any() == false)
            {
                completed = true;
            }
            else
            {
                _personalRecommends.AddRange(result);
            }

            loading = false;
            StateHasChanged();

        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取个性化推荐失败");
        }
    }




    public async Task TriggerDotNetInstanceMethod()
    {
        try
        {
            await JS.InvokeAsync<string>("initInfiniteScroll", objRef);
        }
        catch
        {

        }
    }
    public async ValueTask DisposeAsync()
    {
        objRef?.Dispose();
        try
        {
            await JS.InvokeAsync<string>("deinitInfiniteScroll");
        }
        catch
        {

        }
    }
}
